﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BookStore.Models;
using System.Data;
namespace BookStore.Controllers
{
    [HandleError]
    public class PaymentController : Controller
    {
        public ActionResult Payment()
        {
            PaymentMethodModel pm = new PaymentMethodModel();
            ShippingMethodModel sm = new ShippingMethodModel();
            DataTable result = new DataTable();
            //Get : Payment method//
            if (this.Session["Session_User"] == null)
            {
                return RedirectToAction("Index", "Home");
            }
            if (this.Request.QueryString["type"] != null)
            {
                int type_action = 0;
                try
                {
                    type_action= Convert.ToInt32(this.Request.QueryString["type"]);
                }
                catch (SystemException ex)
                {
                    return RedirectToAction("Index", "Home");
                }
               
                if (type_action == 1)
                {
                    if (this.Session["buy"] == null)
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else if (type_action == 0)
                {
                    if (this.Session["rent"] == null)
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
            try
            {
                result = pm.getAllMethod();
                if (result != null && result.Rows.Count > 0)
                {
                    string[][] payments = new string[result.Rows.Count][];
                    int i = 0;
                    foreach (DataRow row in result.Rows)
                    {
                        payments[i] = new string[2];
                        payments[i][0] = row["payment_id"].ToString();
                        payments[i][1] = row["payment_name"].ToString();
                        i++;
                    }
                    ViewData["payments"] = payments;
                }
                else
                {
                    ViewData["payments"] = null;
                }
            }
            catch (SystemException ex)
            {
                ViewData["payments"] = null;
            }
            //End - Get : Payment method//

            //Get : Shipping method//
            try
            {
                result = sm.getAllMethod();
                if (result != null && result.Rows.Count > 0)
                {
                    string[][] shippings = new string[result.Rows.Count][];
                    int i = 0;
                    foreach (DataRow row in result.Rows)
                    {
                        shippings[i] = new string[3];
                        shippings[i][0] = row["shipping_id"].ToString();
                        shippings[i][1] = row["shipping_name"].ToString();
                        shippings[i][2] = row["cost"].ToString();
                        i++;
                    }
                    ViewData["shippings"] = shippings;
                }
                else
                {
                    ViewData["shippings"] = null;
                }
            }
            catch (SystemException ex)
            {
                ViewData["shippings"] = null;
            }
            ViewData["type_action"] = type_action;
            //End - Get : Shipping method//
            return View();
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }
        public ActionResult PaymentSuccess()
        {
            if (this.Session["Session_User"] == null)
            {
                return RedirectToAction("Index", "Home");
            }
            PaymentMethodModel pm = new PaymentMethodModel();
            ShippingMethodModel sm = new ShippingMethodModel();
            ShippingDetailMethodModel sdm = new ShippingDetailMethodModel();
            BookModel bo = new BookModel();
            int type_action = 0;
            if (this.Request.Form["type_action"] != null)
            {
                try
                {
                    type_action = Convert.ToInt32(this.Request.Form["type_action"]);
                }
                catch (SystemException ex)
                {
                    return RedirectToAction("Index", "Home");
                }
                if (type_action == 1)
                {
                    if (this.Session["buy"] == null)
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else if (type_action == 0)
                {
                    if (this.Session["rent"] == null)
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
                string[] payment = pm.GetMethodById(this.Request.Form["payment_method"].ToString());
                string[] shipping = sm.GetMethodById(this.Request.Form["shipping_method"].ToString());
                string[] shipping_detail = new string[5];
                shipping_detail[0] = shipping[0].ToString();
                shipping_detail[1] = this.Request.Form["name"].ToString().Replace("'", "`");
                shipping_detail[2] = this.Request.Form["address"].ToString().Replace("'", "`");
                shipping_detail[3] = this.Request.Form["phone"].ToString().Replace("'", "`");
                shipping_detail[4] = this.Request.Form["email"].ToString().Replace("'", "`");
                sdm.InsertDetailShipping(shipping_detail);
                string last_sdm = sdm.getLastId();
                long total_bill = Convert.ToInt32(shipping[2].ToString());
                long total_price = 0;
                int quantity = 0;
                long deposit = 0;
                ViewData["shipping"] = shipping;
                ViewData["payment"] = payment;
                ViewData["shipping_detail"] = shipping_detail;
                if (type_action == 1)
                {
                    BillModel bm = new BillModel();
                    BillDetailModel bdm = new BillDetailModel();
                    string[] bill = new string[9];
                    string[][] cart = (string[][])this.Session["buy"];
                    if ((cart != null) && (cart.Length != 0))
                    {
                        foreach (string[] book in cart)
                        {
                            quantity += Convert.ToInt32(book[2].ToString());
                            total_bill += Convert.ToInt64(book[1].ToString());
                            total_price += Convert.ToInt64(book[1].ToString());
                        }
                        bill[0] = last_sdm.ToString();
                        bill[1] = payment[0].ToString();
                        bill[2] = quantity.ToString();
                        bill[3] = total_bill.ToString();
                        bill[4] = Utils.Now().ToString();
                        bill[5] = this.Session["Session_User"].ToString();
                        bill[6] = "Buy" + this.Session["Session_User"].ToString() + Utils.Now().ToString();
                        bill[7] = Utils.Now().ToString();
                        bill[8] = "1";
                        bm.insertNewBill(bill);
                        string last_bill_id = bm.getLastIdBill();
                        string[] bill_detail = new string[6];
                        bill_detail[0] = last_bill_id.ToString();
                        string[] book_detail = new string[15];
                        foreach (string[] book in cart)
                        {
                            book_detail = bo.getBookbyID(Convert.ToInt32(book[0].ToString()));
                            bill_detail[1] = book_detail[6].ToString();
                            bill_detail[2] = book_detail[1].ToString();
                            bill_detail[3] = book_detail[13].ToString();
                            bill_detail[4] = book[2].ToString();
                            bill_detail[5] = (Convert.ToInt32(book[2].ToString()) * Convert.ToInt64(book_detail[13].ToString())).ToString();
                            bdm.InsertBillDetail(bill_detail);
                        }
                        ViewData["quantity"] = quantity;
                        ViewData["total_price"] = total_price;
                        ViewData["total_bill"] = total_bill;
                        this.Session["buy"] = null;
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                    return View();
                }
                else if (type_action == 0)
                {
                    BillRentModel brm = new BillRentModel();
                    BillRentDetailModel brdm = new BillRentDetailModel();
                    string[] bill = new string[10];
                    string[] book_detail = new string[15];
                    string[][] cart = (string[][])this.Session["rent"];
                    if ((cart != null) && (cart.Length != 0))
                    {
                        foreach (string[] book in cart)
                        {
                            book_detail = bo.getBookbyID(Convert.ToInt32(book[0].ToString()));
                            deposit += Convert.ToInt64(book_detail[13].ToString()) * Convert.ToInt32(book[2].ToString());
                            quantity += Convert.ToInt32(book[2].ToString());
                            total_bill += Convert.ToInt64(book[1].ToString());
                            total_price += Convert.ToInt64(book[1].ToString());
                        }
                        total_bill += deposit;
                        bill[0] = last_sdm.ToString();
                        bill[1] = payment[0].ToString();
                        bill[2] = quantity.ToString();
                        bill[3] = total_bill.ToString();
                        bill[4] = Utils.Now().ToString();
                        bill[5] = this.Session["Session_User"].ToString();
                        bill[6] = "Rent" + this.Session["Session_User"].ToString() + Utils.Now().ToString();
                        bill[7] = deposit.ToString();
                        bill[8] = Utils.Now().ToString();
                        bill[9] = "1";
                        brm.insertNewBill(bill);
                        string last_bill_id = brm.getLastIdBill();
                        string[] bill_detail = new string[7];
                        bill_detail[0] = last_bill_id.ToString();

                        foreach (string[] book in cart)
                        {
                            book_detail = bo.getBookbyID(Convert.ToInt32(book[0].ToString()));
                            bill_detail[1] = book_detail[6].ToString();
                            bill_detail[2] = book_detail[1].ToString();
                            bill_detail[3] = book_detail[14].ToString();
                            bill_detail[4] = book[2].ToString();
                            bill_detail[5] = (Convert.ToInt32(book[3].ToString()) * Convert.ToInt32(book[2].ToString()) * Convert.ToInt64(book_detail[14].ToString())).ToString();
                            bill_detail[6] = book[3].ToString();
                            brdm.InsertBillDetail(bill_detail);
                        }
                        ViewData["quantity"] = quantity;
                        ViewData["deposit"] = deposit;
                        ViewData["total_price"] = total_price;
                        ViewData["total_bill"] = total_bill;
                        this.Session["rent"] = null;
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                    return View();
                }
                ViewData["abc"] = 1;
                return View();
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
            return View();
        }
        public ActionResult PaymentFail()
        {
            if (this.Session["Session_User"] == null)
            {
                return RedirectToAction("Index", "Home");
            }
            return View();
        }
        public ActionResult jsonCheckLogin()
        {
            if (this.Session["Session_User"] != null)
            {
                return Content("-1");
            }
            else
            {
                return Content("-2");
            }
        }
    }
}
